
//
//  KYSportMapVC.m
//  kyysprot
//
//  Created by HongYi on 2017/7/1.
//  Copyright © 2017年 kcvly. All rights reserved.
//

#import "KYSportMapVC.h"

#import "KYSportTracking.h"
#import "KYPolyline.h"
#import <MAMapKit/MAMapKit.h>

@interface KYSportMapVC () <MAMapViewDelegate>
@property (nonatomic, weak) MAMapView *mapView;

@end

@implementation KYSportMapVC {
    // 起始位置
    CLLocation *_startLocation;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.mapView;
    NSLog(@"%@", self.mapView);
}

#pragma mark - MAMapViewDelegate
- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation {
    if (!updatingLocation) {
        return ;
    }
    
    if (nil == _startLocation) {
        _startLocation = userLocation.location;
        // 添加大头针
        MAPointAnnotation *pointAnnotation = [[MAPointAnnotation alloc] init];
        pointAnnotation.coordinate = userLocation.coordinate;
        [self.mapView addAnnotation:pointAnnotation];
    } else {
        MAPolyline *polyline = [self.sportTracking polyLineWithLocation:userLocation.location color:[UIColor greenColor]];
        [self.mapView addOverlay:polyline];
    }
}

- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation {
    if (![annotation isKindOfClass:[MAPointAnnotation class]]) {
        return nil;
    }
    
    static NSString *annotationId = @"annotationId";
    MAAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:annotationId];
    if (nil == annotationView) {
        annotationView = [[MAAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationId];
    }
    
    UIImage *sportIcon = self.sportTracking.sportIcon;
    annotationView.image = sportIcon;
    annotationView.centerOffset = CGPointMake(0, -sportIcon.size.height * 0.5);
    
    return annotationView;
}

- (MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id <MAOverlay>)overlay {
    if (![overlay isKindOfClass:[KYPolyline class]]) {
        return nil;
    }
    
    KYPolyline *polyline = (KYPolyline *)overlay;
    MAPolylineRenderer *render = [[MAPolylineRenderer alloc] initWithOverlay:overlay];
    render.lineWidth = 5.0;
    render.lineJoinType = kMALineJoinRound;
    render.lineCapType  = kMALineCapRound;
    render.strokeColor = polyline.color;
    return render;
}

#pragma mark - getter & setter
- (MAMapView *)mapView {
    if (!_mapView) {
        MAMapView *mapView = [[MAMapView alloc] initWithFrame:self.view.bounds];
        mapView.showsScale = NO;
        mapView.rotateCameraEnabled = NO;
        mapView.showsUserLocation = YES;
        mapView.userTrackingMode = MAUserTrackingModeFollow;
        mapView.allowsBackgroundLocationUpdates = YES;
        // 不允许系统暂停位置更新
        mapView.pausesLocationUpdatesAutomatically = NO;
        mapView.delegate = self;
        
        [self.view addSubview:mapView];
        _mapView = mapView;
    }
    return _mapView;
}

@end
